import multer from 'multer'
import fs from 'fs'
import path from 'path'

let createFolder = (folder) => {
    try{
        fs.accessSync(folder); 
    }catch(e){
        fs.mkdirSync(folder);
    }  
};

let uploadFolder =path.join(__dirname,'../public/images');
console.log(uploadFolder);

createFolder(uploadFolder);

// 通过 filename 属性定制
let storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, uploadFolder);    // 保存的路径，备注：需要自己创建
    },
    filename: function (req, file, cb) {
        // 将保存文件名设置为 字段名 + 时间戳，比如 logo-1478521468943
        cb(null, file.originalname);  
    }
});

// 通过 storage 选项来对 上传行为 进行定制化
const upload = multer({ storage: storage });

export default upload;